﻿using LiveCharts;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseUi.ViewModel
{
    public class OutputStatChartViewModel : BindableBase
    {
        public OutputStatChartViewModel()
        {
            Values1.CollectionChanged += Values_CollectionChanged;
            Values2.CollectionChanged += Values_CollectionChanged;
        }

        void Values_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            IsZero = (Values1[0] + Values2[0]) == 0;
        }

        //本地保存路径
        public static readonly string DailyStatisticsPath = System.IO.Path.Combine(LocalFile.DataDir, "outputStat");
        //文件锁
        static object lockObject = new object();

        #region 图表
        public ChartValues<int> Values1 { get; set; } = new ChartValues<int>() { 10 };
        public ChartValues<int> Values2 { get; set; } = new ChartValues<int>() { 2 };
        public Func<ChartPoint, string> PointLabel { get; set; } = value => $"{value.Y}";
        #endregion

        private string Title_ = "当日生产统计";
        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get => Title_; set { SetProperty(ref Title_, value); } }

        private bool IsZero_ = true;
        /// <summary>
        /// 是否为0
        /// </summary>
        public bool IsZero { get => IsZero_; set { SetProperty(ref IsZero_, value); } }

        private DateTime? StartTime_ = null;
        /// <summary>
        /// 开始时间
        /// </summary>
        public DateTime? StartTime
        {
            get => StartTime_;
            set
            {
                StartTime_ = value;
                Title = !value.HasValue ? "当日生产统计" : value.Value == DateTime.Now.Date ? "当日生产统计" : $"生产统计({value.Value.ToString(@"yyyy/MM/dd")})";
            }
        }

        public void AddOkNg(int okNum, int ngNum)
        {
            if (okNum < 1 && ngNum < 1)
                return;

            if (StartTime == null)
                return;

            //重新计算
            if (StartTime.Value.Date != DateTime.Now.Date)
                Reset();

            if (okNum > 0)
                Values1[0] = Values1[0] + okNum;
            if (ngNum > 0)
                Values2[0] = Values2[0] + ngNum;

            SaveData();
        }

        public void AddOk(int num = 1)
        {
            if (num < 1)
                return;

            if (StartTime == null)
                return;

            //重新计算
            if (StartTime.Value.Date != DateTime.Now.Date)
                Reset();

            Values1[0] = Values1[0] + num;
            SaveData();
        }

        public void AddNg(int num = 1)
        {
            if (num < 1)
                return;

            if (StartTime == null)
                return;

            //重新计算
            if (StartTime.Value.Date != DateTime.Now.Date)
                Reset();

            Values2[0] = Values2[0] + num;
            SaveData();
        }

        public void UpdataData()
        {
            try
            {
                if (!File.Exists(DailyStatisticsPath))
                {
                    Values1[0] = 0;
                    Values2[0] = 0;
                    StartTime = DateTime.Now.Date;
                }
                else
                {
                    JObject? jo;
                    lock (lockObject)
                        jo = JsonConvert.DeserializeObject<JObject>(File.ReadAllText(DailyStatisticsPath, Encoding.UTF8));

                    if (jo == null)
                    {
                        Values1[0] = 0;
                        Values2[0] = 0;
                        StartTime = DateTime.Now.Date;
                    }
                    else
                    {
                        Values1[0] = jo.Value<int>("ok");
                        Values2[0] = jo.Value<int>("ng");
                        StartTime = jo.Value<DateTime>("dt");
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        public void SaveData()
        {
            try
            {
                var data = new { ok = Values1[0], ng = Values2[0], dt = StartTime };
                lock (lockObject)
                    File.WriteAllText(DailyStatisticsPath, JsonConvert.SerializeObject(data), Encoding.UTF8);
            }
            catch (Exception)
            {

            }
        }

        public void Reset()
        {
            Values1[0] = 0;
            Values2[0] = 0;
            StartTime = DateTime.Now.Date;
        }
    }
}
